package com.bang.framework.mq.config;


import com.bang.framework.mq.constants.QueueConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: MqConfiguration
 * @author: BANG
 * @Date: 2019/12/06 17:48
 * @Version: 1.0
 * @Modified By:
 * @Desc: TODO
 */

@Configuration
public class MqConfiguration {


    /**
     * 默认queue
     * @return
     */
    @Bean
    Queue defaultQueue() {
       return QueueBuilder.durable(QueueConstants.Queue.BANG_TEST_DEFAULT_QUEUE).build();
    }

    /**
     * 直接
     * @return
     */
    @Bean
    Exchange directExchange() {
        return ExchangeBuilder.directExchange(QueueConstants.Exchange.BANG_TEST_DIRECT_EXCHANGE).durable(true).build();
    }


    @Bean
    Queue directQueue() {
        return QueueBuilder.durable(QueueConstants.Queue.BANG_TEST_DIRECT_QUEUE).build();
    }

    @Bean
    public Binding directBind() {
        return BindingBuilder
                .bind(directQueue())
                .to(directExchange())
                .with(QueueConstants.RoutingKey.BANG_TEST_DIRECT_ROUTEKEY).noargs();
    }


    @Bean
    Exchange topicExchange() {
        return ExchangeBuilder.topicExchange(QueueConstants.Exchange.BANG_TEST_TOPIC_EXCHANGE).durable(true).build();
    }

    @Bean
    Queue topicQueue1() {
        return QueueBuilder.durable(QueueConstants.Queue.BANG_TEST_TOPIC_QUEUE_1).build();
    }

    @Bean
    Queue topicQueue2() {
        return QueueBuilder.durable(QueueConstants.Queue.BANG_TEST_TOPIC_QUEUE_2).build();
    }

    @Bean
    public Binding topicBind1() {
        return BindingBuilder
                .bind(topicQueue1())
                .to(topicExchange())
                .with(QueueConstants.RoutingKey.BANG_TEST_TOPIC_ROUTEKEY).noargs();
    }

    @Bean
    public Binding topicBind2() {
        return BindingBuilder
                .bind(topicQueue2())
                .to(topicExchange())
                .with(QueueConstants.RoutingKey.BANG_TEST_TOPIC_ROUTEKEY).noargs();
    }



    @Bean
    FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(QueueConstants.Exchange.BANG_TEST_FANOUT_EXCHANGE).durable(true).build();
    }

    @Bean
    Queue fanoutQueue1() {
        return QueueBuilder.durable(QueueConstants.Queue.BANG_TEST_FANOUT_QUEUE_1).build();
    }

    @Bean
    Queue fanoutQueue2() {
        return QueueBuilder.durable(QueueConstants.Queue.BANG_TEST_FANOUT_QUEUE_2).build();
    }


    @Bean
    public Binding fanoutBind1() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBind2() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }


    // ttl消息

    /**
     * update
     *
     */
    /**
     * 消息中心实际消费队列交换配置
     *
     * @return
     */
    @Bean
    DirectExchange billSummaryDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueConstants.TCC_BILL_SUMMARY_DIRECT_EXCHANGE)
                .durable(true)
                .build();
    }

    /**
     * 消息中心延迟消费交换配置
     *
     * @return
     */
    @Bean
    DirectExchange billSummaryTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueConstants.TCC_BILL_SUMMARY_TTL_DIRECT_EXCHANGE)
                .durable(true)
                .build();
    }

    /**
     * 消息中心实际消费队列配置
     *
     * @return
     */
    @Bean
    public Queue billSummaryQueue() {
        return new Queue(QueueConstants.TCC_BILL_SUMMARY_QUEUE);
    }


    /**
     * 消息中心TTL队列
     *
     * @return
     */
    @Bean
    Queue billSummaryTtlQueue() {
        return QueueBuilder
                .durable(QueueConstants.TCC_BILL_SUMMARY_TTL_QUEUE)
                // 配置到期后转发的交换
                .withArgument("x-dead-letter-exchange", QueueConstants.TCC_BILL_SUMMARY_DIRECT_EXCHANGE)
                // 配置到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueConstants.TCC_BILL_SUMMARY_ROUTE_KEY)
                .build();
    }

    /**
     * 消息中心实际消息交换与队列绑定
     *
     * @return
     */
    @Bean
    Binding billSummaryBind() {
        return BindingBuilder
                .bind(billSummaryQueue())
                .to(billSummaryDirect())
                .with(QueueConstants.TCC_BILL_SUMMARY_ROUTE_KEY);
    }

    /**
     * 消息中心TTL绑定实际消息中心实际消费交换机
     *
     * @return
     */
    @Bean
    public Binding billSummaryTtlBind() {
        Binding with = BindingBuilder
                .bind(billSummaryTtlQueue())
                .to(billSummaryTtlDirect())
                .with(QueueConstants.TCC_BILL_SUMMARY_TTL_ROUTE_KEY);
        return with;
    }


}
